
//alert(new Date()).Format("yyyy-MM-dd hh:mm:ss.S"));
var userId;
var userName;
var roomId =-1;
var userType =-1;
var lat;
var lon;
var url;
var url8;

var runState =-1;
var lifeUserName;
var lifeuserId;

var app = new Vue({
            el: '#app',
            data: {
                cut: false,
				showFastMessageWindow:false,
				ShowManagerUI:false,
				showlife:false,
				//蓝牙状态
				blueToothState:false,
				dis:true,
				buttonName:"管理战队",
				timeShow:"游戏剩时：加载中...",
                msg: {
                    bulletValue: 0,
					bulletName: "弹量信息0/0",
                    scoreValue: 0,
					scoreName: "战绩信息0",
                    lifeValue: 0,
					 lifeName: "生命值0/0",
                },
				fastMessagecontent:["我来了","集合","注意隐蔽","卧倒","攻击","前进","后退","开火","冲锋"],
				
                list: [
                ],
				currentUser:"当前用户:加载中",
				goldCoinName:"金币数量:0",
				user_room:"",
				inputContent:"",
				onLineCount:"用户在线数量：0",
            },
            mounted() {
				
               setInterval(() => {
				  ChckeScope();
                }, 30000)
				
            },
            methods: {
                receive() {
                    let index = this.list.length
                    this.list.push({
                        name: `第${index}条的数据`
                    })
                },
                delete() {
                    this.list.splice(0, 1);
                },
                send() { //发送
						
						this.showFastMessageWindow =false;
						if (this.inputContent == '') {
							mui.alert('输入内容不能为空!',function (){});
							return ;
						}
						
						var requestUrl = url+"SendChatMessage/"+roomId+"|"+this.inputContent+"|"+
						userName +"|"+userType+"|"+userId;
						ShowCurrrentMessage();
						axios.get(requestUrl)
						  .then(function (response) {
							  //that.inputContent = "";
						    //mui.toast('操作成功!',{ duration:'long', type:'div' })
							 //mui.alert('操作成功');
						  })
						  .catch(function (error) {
						    console.log(error);
						  });
						  
						  
                },
                isShow() {
                    this.cut = !this.cut;
					this.showFastMessageWindow = false;
                },
				FastChange()
				{
					this.showFastMessageWindow =!this.showFastMessageWindow;
				},
				CloseMessageWindow()
				{
					this.showFastMessageWindow = false;
				},
				SelectMessage(message){
					this.inputContent = message;
				},
				OpenManagerPlayer()
				{
					//玩家
					if(userType==0)
					{
						document.getElementById("playerUIApp").style.left ="0px";
						GetPlayerLifeListInfo();
					}
					else
					{
						appManager.ShowManagerUI = !appManager.ShowManagerUI;
						if(appManager.ShowManagerUI)
						{
							if(!isTest)
							{
								window.location.href = "uniwebview://GetRoomList?userId=" + userId;
							}
							
						}
						SetAdminButtonName();
					}
					
					
				}
            }
        })
		
		function SetAdminButtonName()
		{
			if(appManager.ShowManagerUI)
			{
				app.buttonName = "关闭管理";
			}
			else{
				app.buttonName = "战队管理";
			}
		}
		
		//获取玩家的生命值列表
		function GetPlayerLifeListInfo()
		{
			if(!isTest)
			{
				window.location.href = "uniwebview://ShowPlayInfo?userId=" + userId;
			}
			
		}
		
		function ChatMessage(message)
		{
			
			var content = message.name +":"+message.content + "   ["+message.time +"]";
			if(content.length>30)
			{
				content = content.substring(0,30);
			}
			app.inputContent ="";
			app.list.push({
                        name: content
                    })
					
		   
		   if(app.list.length>6)
		   {
			   app.list.splice(0, 1);
		   }
		}
		
		var _preData  = null;
		function SetLifeMesage(data)
		{
			app.dis = false;
			//玩家
			if(data.currentUser.userType == 0) {
				app.showlife = true;
				app.currentUser = "我的战队："+data.room.name +"/"+data.currentUser.userName;
				app.goldCoinName = "金币数量："+data.life.goldCoin+"/100";
				
				app.msg.bulletValue = (data.life.bulletCount/data.life.setBulletCount)*100;
				app.msg.bulletName = "弹量数量："+data.life.bulletCount+"/"+data.life.setBulletCount;
				
				app.msg.scoreValue = data.life.fightScore;
				app.msg.scoreName = "我的战绩："+data.life.fightScore+"/100";
				
				app.msg.lifeValue = (data.life.lifeValue/data.life.setLifeValue)*100;
				if(data.life.lifeValue<0){
					data.life.lifeValue = 0;
				}
				
				app.msg.lifeName = "命值数量："+data.life.lifeValue+"/"+data.life.setLifeValue;
				app.buttonName = "我的设置";
			}
			else
			{
				app.currentUser = "管理员："+data.currentUser.userName;
				app.showlife = false;
				SetAdminButtonName();
			}
			this._preData = data;
		}
		//减少弹量
		function ReduceBullet()
		{
			if(_preData!=null)
			{
				app.msg.bulletName = "弹量数量："+(_preData.life.bulletCount-1)+"/"+_preData.life.setBulletCount;
			}
		}
		
		//减少命值
		function ReduceLife()
		{
			if(_preData!=null)
			{
				app.msg.lifeName = "命值数量："+(_preData.life.lifeValue-1)+"/"+_preData.life.setLifeValue;
			}
		}
		
		//增加战绩
		function AddFightScore()
		{
			if(_preData!=null)
			{
				app.msg.scoreName = "我的战绩："+(_preData.life.fightScore+1)+"/100";
			}
			
		}
		
		
		function SetOnLineCount(data)
		{
			//管理端
			if(data.currentUser.userType == 1)
			{
				let count = data.gpsData==null?0:data.gpsData.length;
				app.onLineCount = "用户在线数量："+count;
			}
			
		}
		
		function SetUserValue(data)
		{
			
			url ="http://" + data.ip + ":8899/" ;
			url8 = "http://" + data.ip + ":8898/" ;
			if("undefined" != typeof data.currentUser){ 
				this.userId = data.currentUser.userId;
				this.userName = data.currentUser.userName;
				this.userType = data.currentUser.userType;			
				this.lat = data.currentUser.lat;
				this.lon =data.currentUser.lon;
				
			}
			
			if("undefined" != typeof data.room && data.room!=null){
				
				this.roomId = data.room.id;
			}
			if("undefined" != typeof data.grounp && data.grounp!=null){
				
				this.runState = data.grounp.runState;		
			}
		
		}
		
		function GetNowFormatDate() {//获取当前时间
			var date = new Date();
			var seperator2 = ":";
			currentdate = 
					handleTime(date.getHours())  + seperator2  + handleTime(date.getMinutes())
					+ seperator2 + handleTime(date.getSeconds());
			return currentdate;
		}
		
		//当时间单位为个位数字时候，前边补0
		function handleTime(result)
		{
			if(result<10)
			{
				return "0"+result;
			}
			return result;
					
		}
		
		function ShowCurrrentMessage()
		{
			var _name = this.userName;
			if(this.userType!=0)
			{
				_name = "管理员";
			}
			
			var json =
			{
				name:_name,
				content:this.app.inputContent,
				time:GetNowFormatDate()
			}
			ChatMessage(json);
			
		}
		
		//检查范围
		function ChckeScope()
		{
		
			if(userType==0 && runState==0)
			{
				var myLngLat = new AMap.LngLat(this.lon,this.lat);
				if(!circle.contains(myLngLat)){
					
					var requestUrl = url8+"SubtractLife/"+userId;
					axios.get(requestUrl)
					  .then(function (response) {
						PlaySound("fence");
					  })
					  .catch(function (error) {
					    //console.log(error);
					  });
				}
			}
		}
		
		///设置设备状态
		function SetBlueToothState(state)
		{
			//连接正常
			if(state==0)
			{
				app.blueToothState = true;
			}
			else
			{
				app.blueToothState = false;
			}
		}